function TeaMinderAssistant(selectedTea) {
	this.selectedTea = selectedTea;
}

TeaMinderAssistant.prototype.setup = function() {
	this.controller.listen($('start_timer'), Mojo.Event.tap, this.handelStartTimer.bind(this));
};

TeaMinderAssistant.prototype.activate = function(event) {
	$("type").innerHTML = this.selectedTea.type;
	$("amount").innerHTML = this.selectedTea.amount;
	$("strength").innerHTML = this.selectedTea.strength;
	
	
	$('timer').innerHTML = ((this.selectedTea.mins <10) ? "0" + this.selectedTea.mins : this.selectedTea.mins) + ":" + ((this.selectedTea.secs < 10) ? "0"+ this.selectedTea.secs : this.selectedTea.secs);
};


TeaMinderAssistant.prototype.deactivate = function(event) {
	
};

TeaMinderAssistant.prototype.cleanup = function(event) {
	clearInterval(this.interval);
};

TeaMinderAssistant.prototype.updateTimer = function()
{
	var time = new Date();

	var secs = (120 + this.targetTime.getSeconds() - time.getSeconds()) % 60;	
	var mins = this.targetTime.getMinutes() - time.getMinutes();
	if(secs > this.targetTime.getSeconds())
		mins -= 1;

	if(mins <= 0 && secs <= 3)
	{
		$('timer').innerHTML = "DONE!!";
		clearInterval(this.interval);

		var appController = Mojo.Controller.getAppController();
		    
		    var f = function(stageController){
				//We can't use our showScene function from our app's stage assistant here since this
				//stageController is actually a new stage controller specifically for our popup alert.
		        stageController.pushScene({name: "popupWindow", sceneTemplate: "popupWindow/popupWindow-scene"}, "Your tea is ready!!");
		    };
		    appController.createStageWithCallback({
		        name: "popup",
		        height: 125
		    }, f, 'popupalert');
	}
	else
	{
		$('timer').innerHTML = ((mins <10) ? "0" + mins : mins) + ":" + ((secs < 10) ? "0"+ secs : secs);
	}
};

TeaMinderAssistant.prototype.handelStartTimer = function(event)
{

	var currentTime = new Date();
	this.targetTime = new Date();
	this.targetTime.setMinutes(currentTime.getMinutes() + this.selectedTea.mins);	
	this.targetTime.setSeconds(currentTime.getSeconds() + this.selectedTea.secs);
	$("date").innerHTML = "Started at: " + currentTime.toLocaleTimeString(); 
	$("date").innerHTML += "<br/>Ready at: " + this.targetTime.toLocaleTimeString();
	
	this.updateTimer();	

	var that = this;
	this.interval = window.setInterval(function () {
		that.updateTimer();
		
	}, 1000);
};

